package org.terot.framework.fuzzy.instance;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.terot.util.Logg;

final class PrimitiveInstantiator {
	
	private PrimitiveInstantiator() {}
	
	/**
	 * 
	 * @param <T>
	 * @param cl
	 * @return
	 */
	protected static<T> Collection<T> generatePrimitives(Class<T> cl) {
		
		List<T> list = new ArrayList<T>(); 
		
        Logg.info("Creating instance of " + cl.getName());

        Logg.info("Checking for primitive types");
        if (cl.isPrimitive() || cl == String.class ) try {
            
        	list.add(createInstance(cl));
            
        } catch (IllegalAccessException e) {
            Logg.error("Primitive instantiation failed; " + e.getMessage());
        } catch (InvocationTargetException e) {
            Logg.error("Primitive instantiation failed: " + e.getMessage());
        }

        return list;
	}
	
	/**
	 * Create an unset fuzzy instance for a primitive type
	 * @param cl
	 * @param x
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	@SuppressWarnings("unchecked")
	private static<T> T createInstance(Class<T> cl) throws IllegalAccessException, InvocationTargetException {
	    
    	Object obj = null;
    	
    	if (cl == Integer.TYPE) {
		
			obj = new Integer((new Random()).nextInt());
		} else if (cl == String.class) {
			
			obj = Integer.toString( (new Random()).nextInt(), 27 ); 
		} else if (cl == Double.class) {
			
			obj = (new Random()).nextDouble();
		} 
    	
    	return (T) obj;
	}

}
